package com.actions;

import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.bean.ProductPart;
import com.opensymphony.xwork2.ActionSupport;
import com.serviceImpl.ProductPartServiceImpl;
import com.serviceImpl.UtilSupport;

public class ProductPartAction extends ActionSupport{

	private ProductPartServiceImpl productPartBiz;
	private UtilSupport util ;
	private boolean flag;
	
	private List<ProductPart> productPartList;	//产品部位集合（根据条件查询）
	
	private ProductPart productPart;	//产品部位
	private boolean isDraftPart;	//是否保存草稿

	private int offset;			//当前页
	private int pageSize=10;
	private int totalcount;		// 总记录数
	private int totalpage; 		// 总页数
	
	/**
	 * 查询条件
	 */
	private String qpartCode;		//部位编码
	private String qproductCode;	//参考产品编码
	private Integer qaplId;		//部位ID
	private Timestamp qscreateTime;	//创建时间（start）
	private Timestamp qecreateTime;	//创建时间（end）
	
	public ProductPartAction() {
		super();
	}

	public ProductPartServiceImpl getProductPartBiz() {
		return productPartBiz;
	}

	public void setProductPartBiz(ProductPartServiceImpl productPartBiz) {
		this.productPartBiz = productPartBiz;
	}

	public UtilSupport getUtil() {
		return util;
	}

	public void setUtil(UtilSupport util) {
		this.util = util;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public List<ProductPart> getProductPartList() {
		return productPartList;
	}

	public void setProductPartList(List<ProductPart> productPartList) {
		this.productPartList = productPartList;
	}

	public int getOffset() {
		return offset;
	}

	public void setOffset(int offset) {
		this.offset = offset;
	}

	public int getTotalcount() {
		return totalcount;
	}

	public void setTotalcount(int totalcount) {
		this.totalcount = totalcount;
	}

	public int getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}

	public String getQpartCode() {
		return qpartCode;
	}

	public void setQpartCode(String qpartCode) {
		this.qpartCode = qpartCode;
	}

	public String getQproductCode() {
		return qproductCode;
	}

	public void setQproductCode(String qproductCode) {
		this.qproductCode = qproductCode;
	}

	public Integer getQaplId() {
		return qaplId;
	}

	public void setQaplId(Integer qaplId) {
		this.qaplId = qaplId;
	}

	public Timestamp getQscreateTime() {
		return qscreateTime;
	}

	public void setQscreateTime(Timestamp qscreateTime) {
		this.qscreateTime = qscreateTime;
	}

	public Timestamp getQecreateTime() {
		return qecreateTime;
	}

	public void setQecreateTime(Timestamp qecreateTime) {
		this.qecreateTime = qecreateTime;
	}

	public ProductPart getProductPart() {
		return productPart;
	}

	public void setProductPart(ProductPart productPart) {
		this.productPart = productPart;
	}

	public boolean isDraftPart() {
		return isDraftPart;
	}

	public void setDraftPart(boolean isDraftPart) {
		this.isDraftPart = isDraftPart;
	}

	/**
	 * 获取所有产品信息
	 * @return
	 */
	public String getPpartListByOptions(){
		this.partList();
		return "show";
	}

	/**
	 * 获取部位信息集合
	 * @return
	 */
	private List<ProductPart> partList(){
		HttpServletRequest request = ServletActionContext.getRequest();
 		StringBuffer sqlbf = new StringBuffer("SELECT a.*,b.part_name,b.part_describe from audit.product_part a INNER JOIN audit.applicable_part b ON a.apl_id = b.apl_id where 0=0 ");
 		
 		StringBuffer querySql = new StringBuffer("");
 		
 		try {
			this.qpartCode = request.getParameter("qpartCode");
			if(qpartCode!=null&&!qpartCode.isEmpty()){
				qpartCode=new String(qpartCode.trim().getBytes("ISO-8859-1"),"UTF-8");
				String query = " and a.part_code like '%"+qpartCode+"%'";
				querySql.append(query);
				sqlbf.append(query);
			}

			this.qproductCode = request.getParameter("qproductCode");
			if(qproductCode!=null&&!qproductCode.isEmpty()){
				qproductCode=new String(qproductCode.trim().getBytes("ISO-8859-1"),"UTF-8");
				
				String query = " and a.product_code like ''%"+qproductCode+"%''";
				querySql.append(query);
				sqlbf.append(query);
			}
			
			String aplIdStr = request.getParameter("qaplId");
			if(aplIdStr!=null&&!aplIdStr.isEmpty()){
				this.qaplId = Integer.parseInt(aplIdStr);
				
				String query = " and a.apl_id = '+qaplId+'";
				querySql.append(query);
				sqlbf.append(query);
			}
			
			String scdate=request.getParameter("qscreateTime");
			String ecdate=request.getParameter("qecreateTime");
			

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 设置时间显示格式
			
			if(scdate!=null&&!scdate.isEmpty()){
				//设置开始时间
				Date sDate = sdf.parse(scdate);
				qscreateTime = new Timestamp(sDate.getTime());
				
				if(ecdate!=null&&!ecdate.isEmpty()){
					//设置结束时间
					Date eDate = sdf.parse(ecdate);
					qecreateTime = new Timestamp(eDate.getTime());
					
					querySql.append(" and a.create_time between '"+qscreateTime+"' and '"+qecreateTime+"'");
				}else{
					querySql.append(" and a.create_time > '"+qscreateTime+"'");
				}
				
			}else{
				if(ecdate!=null&&!ecdate.isEmpty()){
					//设置结束时间
					Date eDate = sdf.parse(ecdate);
					qecreateTime = new Timestamp(eDate.getTime());
					
					querySql.append(" and a.create_time < '"+qecreateTime+"'");
				}
			}

			String sql =sqlbf.toString() + querySql.toString();
			
			totalcount = util.getTotalCount(sql);

			totalpage = totalcount % pageSize == 0 ? totalcount / pageSize
					: totalcount / pageSize + 1;

			offset = getPageOffset();
			//表名
			String table="v_craft_part";
			//主键
			String pk="part_id";
			//排序字段
			String sort="create_time";
			//当前页数
			int PageNumber = offset+1;
			int PageSize = pageSize;
			if(querySql.toString().equals("")){
				querySql.append(" 0=0");
			}
			
			productPartList = productPartBiz.findProductParts(table, pk, sort, PageNumber, PageSize, querySql.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return productPartList;
	}
	
	// Added by JSL : 获取翻页偏移量(实际上是将要翻到的页面的页索引，页索引从 0 开始)
	private int getPageOffset() {
		HttpServletRequest request=ServletActionContext.getRequest();
		String ofst = request.getParameter("offset");
		int idx = 0;
		if(ofst!=null){
			idx = Integer.valueOf(ofst);
			idx = idx < 0 ? 0 : idx;                        // 超过第一页时，不再翻页
			idx = idx >= totalpage ? (totalpage-1) : idx;	// 超过最后一页时，不再翻页		
		}
		return idx;
	}
	
	/**
	 * 获取部位（选择部位）
	 * @return
	 */
	public String getpart(){
		
		this.partList();
		
		return "choose";
	}
	
	/**
	 * 保存产品部位草稿
	 * @return
	 */
	public String draftSavePart(){
		try {
			productPart = new ProductPart();
			productPartBiz.saveProductPart(productPart);
			flag = true;
		} catch (Exception e) {
			flag = false;
		}
		return SUCCESS;
	}
	
	/**
	 * 保存产品部位信息
	 * @return
	 */
	public String saveProudctPart(){
		HttpServletRequest request=ServletActionContext.getRequest();
		
		try {
			String aaa=new String(request.getParameter("newPwd").trim().getBytes("iso-8859-1"),"utf-8");
			
			if(!isDraftPart){
				productPart = new ProductPart();
				productPartBiz.saveProductPart(productPart);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return "save";
	}
}
